from os.path import join, exists
from config import *
from Document import Document

class Lexicon:

	'''
	整个数据集的词典表示
	words are 1-indexed.
	'''

	def create(self):
		word_dict = {}
		for doc in xrange(doccount):
			print 'Processing %d' % doc
			paper = Document(doc)
			comments = ''
			for com in paper.getComments():
				comments = comments + com + ' '
			content = ' '.join([paper.getTitle(), paper.getAbstract(), paper.getBody(), comments])
			words = content.split()
			for word in words:
				if not word_dict.has_key(word):
					word_dict[word] = True
			f = open(lexicon_path, 'w')
			for word in word_dict.keys():
				f.write(word + '\n')
			f.close()
	
	def load(self):
		self.term2id = {}
		self.id2term = {}
		word_id = 1

		f = open(lexicon_path, 'r')
		words = f.readlines()
		f.close()

		for word in words:
			word = word.strip()
			self.term2id[word] = word_id
			self.id2term[word_id] = word
			word_id = word_id + 1
	
	#通过词找编号
	def getIdByTerm(self, term):
		return self.term2id[term]
	#通过编号找词
	def getTermById(self, wordid):
		return self.id2term[wordid]
	#得到词典的大小
	def getSize(self):
		return len(self.term2id)

if __name__ == '__main__':
	lexicon = Lexicon()

	#lexicon.create()
	lexicon.load()

	print lexicon.getIdByTerm('music')
	print lexicon.getTermById(2563)
	print lexicon.getSize()
